﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Reflection;

namespace 玩具商城管理系统
{
    public partial class newwin : Form
    {
        public newwin()
        {
            InitializeComponent();
        }
        public static int flag = 1;
        public static bool Aflag = false;//false为未登录，true为已登录
        public static string Commodityid = "";
        SqlDataAdapter daProduct;
        void showAll1()
        {
           DB.GetCn();
            string str = "SELECT c.*, h.count\r\n    FROM commodity c\r\n    JOIN (\r\n        SELECT TOP 10 hot_commodityname, sum(hot_commoditynum) AS count\r\n        FROM hot\r\n        WHERE hot_status = '已审核'\r\n        GROUP BY hot_commodityname\r\n        ORDER BY COUNT(*) DESC\r\n    ) h ON c.commodity_name = h.hot_commodityname";
            SqlCommand cmd = new SqlCommand(str,DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[0].Value = rdr[0];
                    dataGridView1.Rows[index].Cells[1].Value = rdr[1];
                    dataGridView1.Rows[index].Cells[2].Value = rdr[2];
                    dataGridView1.Rows[index].Cells[3].Value = rdr[3];
                    dataGridView1.Rows[index].Cells[4].Value = rdr[9];
               
            }
            rdr.Close();
            // 指定排序的方式为降序
            System.Data.SqlClient.SortOrder order = System.Data.SqlClient.SortOrder.Descending;

            // 设置指定列的排序模式为程序排序
            dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.Programmatic;

            // 调用 DataGridView 的 Sort 方法来排序
            dataGridView1.Sort(dataGridView1.Columns[4], (ListSortDirection)order);
            
        }
        void showAll2()
        {
            DB.GetCn();
            string str2 = "select * from commodity where commodity_name like '%枪%'";
            SqlCommand cmd = new SqlCommand(str2, DB.cn);
            SqlDataReader rdr2 = cmd.ExecuteReader();
            while (rdr2.Read())
            {
                int index = dataGridView2.Rows.Add();
                dataGridView2.Rows[index].Cells[0].Value = rdr2[0];
                dataGridView2.Rows[index].Cells[1].Value = rdr2[1];
                dataGridView2.Rows[index].Cells[2].Value = rdr2[2];
                dataGridView2.Rows[index].Cells[3].Value = rdr2[3];
                dataGridView2.Rows[index].Cells[4].Value = Convert.ToDouble(rdr2[2]) * 1.2; ;
            }
            rdr2.Close();
        }
        void showAll3()
        {
            DB.GetCn();
            string str3 = "select * from commodity where commodity_invent<=50";
            SqlCommand cmd = new SqlCommand(str3, DB.cn);
            SqlDataReader rdr3 = cmd.ExecuteReader();
            while (rdr3.Read())
            {
                int index = dataGridView3.Rows.Add();
                dataGridView3.Rows[index].Cells[0].Value = rdr3[0];
                dataGridView3.Rows[index].Cells[1].Value = rdr3[1];
                dataGridView3.Rows[index].Cells[2].Value = rdr3[2];
                dataGridView3.Rows[index].Cells[3].Value = rdr3[3];
                dataGridView3.Rows[index].Cells[4].Value = "剩余10天";
            }
            rdr3.Close();
        }
        
        

        void label5style()
        {
            
        }
        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void newwin_Load(object sender, EventArgs e)
        {
            showAll1();
            showAll2();
            showAll3();
            label5style();
            data1.ForeColor = Color.Blue;
            dataGridView1.Visible = true;
            dataGridView2.Visible = false;
            dataGridView3.Visible = false; 
        }

        private void data1_Click(object sender, EventArgs e)
        {
            data1.ForeColor = Color.Blue;
            data2.ForeColor = Color.Crimson;
            data3.ForeColor = Color.Crimson;
            dataGridView1.Visible = true;
            dataGridView3.Visible = false;
            dataGridView2.Visible = false;
        }

        private void data2_Click(object sender, EventArgs e)
        {
            data1.ForeColor = Color.Crimson;
            data2.ForeColor = Color.Blue;
            data3.ForeColor = Color.Crimson;
            dataGridView1.Visible = false;
            dataGridView3.Visible = false;
            dataGridView2.Visible = true;
        }

        private void data3_Click(object sender, EventArgs e)
        {
            data1.ForeColor = Color.Crimson;
            data2.ForeColor = Color.Crimson;
            data3.ForeColor = Color.Blue;
            dataGridView1.Visible = false;
            dataGridView2.Visible = false;
            dataGridView3.Visible = true;
        }

        private void btnfind_Click(object sender, EventArgs e)//查询商品
        {
            if(dataGridView3.Visible==true)
            {
                if (txtfind.Text == "")
                {
                    MessageBox.Show("请输入查询关键字");
                }
                else 
                {
                    dataGridView3.Rows.Clear();
                    DB.GetCn();
                    string str = "select * from commodity where commodity_name like '%" + txtfind.Text + "%' and commodity_invent<=50";
                    SqlCommand cmd = new SqlCommand(str, DB.cn);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        int index = dataGridView3.Rows.Add();
                        dataGridView3.Rows[index].Cells[0].Value = rdr[0];
                        dataGridView3.Rows[index].Cells[1].Value = rdr[1];
                        dataGridView3.Rows[index].Cells[2].Value = rdr[2];
                        dataGridView3.Rows[index].Cells[3].Value = rdr[3];
                        dataGridView3.Rows[index].Cells[4].Value = "剩余10天";

                    }
                    rdr.Close();
                }
            }
            else if(dataGridView2.Visible == true)
            {
                    if (txtfind.Text == "")
                    {
                        MessageBox.Show("请输入查询关键字");
                    }
                    else
                    {
                        dataGridView2.Rows.Clear();
                        DB.GetCn();
                        string str = "select * from commodity where commodity_name like '%" + txtfind.Text + "%' and commodity_name like '%枪%'";
                        SqlCommand cmd = new SqlCommand(str, DB.cn);
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            int index = dataGridView2.Rows.Add();
                            dataGridView2.Rows[index].Cells[0].Value = rdr[0];
                            dataGridView2.Rows[index].Cells[1].Value = rdr[1];
                            dataGridView2.Rows[index].Cells[2].Value = rdr[2];
                            dataGridView2.Rows[index].Cells[3].Value = rdr[3];
                            dataGridView2.Rows[index].Cells[4].Value = Convert.ToDouble(rdr[2]) * 1.2; ;
                    }
                        rdr.Close();
                    }
                }
            else if(dataGridView1.Visible==true)
            {
                if (txtfind.Text == "")
                {
                    MessageBox.Show("请输入查询关键字");
                }
                else
                {
                    dataGridView1.Rows.Clear();
                    DB.GetCn();
                    string str = "SELECT c.*, h.count\r\n    FROM commodity c\r\n    JOIN (\r\n        SELECT TOP 10 hot_commodityname, sum(hot_commoditynum) AS count\r\n        FROM hot\r\n        WHERE hot_status = '已审核'\r\n        GROUP BY hot_commodityname\r\n        ORDER BY COUNT(*) DESC\r\n    ) h ON c.commodity_name = h.hot_commodityname where commodity_name like '%" + txtfind.Text + "%'";
                    SqlCommand cmd = new SqlCommand(str, DB.cn);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        int index = dataGridView1.Rows.Add();
                        dataGridView1.Rows[index].Cells[0].Value = rdr[0];
                        dataGridView1.Rows[index].Cells[1].Value = rdr[1];
                        dataGridView1.Rows[index].Cells[2].Value = rdr[2];
                        dataGridView1.Rows[index].Cells[3].Value = rdr[3];
                        dataGridView1.Rows[index].Cells[4].Value = rdr[9];

                    }
                    rdr.Close();
                }
            }
            else
            {
                return;
            }
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            dataGridView2.Rows.Clear();
            dataGridView3.Rows.Clear();
            showAll1();
            showAll2();
            showAll3();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            login ll = new login();
            ll.ShowDialog();
            if (flag == 1)
            {
                label5.Text = login.username;
                DB.GetCn();
                string sdr2 = "";
                string str = "select * from client where client_name='" + login.username + "'";
                SqlCommand cmd = new SqlCommand(str, DB.cn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(10))
                    {
                        sdr2 = (string)rdr[10];
                    }
                   
                    string sdr = (string)rdr[8];
                    if (sdr == "高级用户")
                    {
                        label5.ForeColor = Color.DarkGoldenrod;
                        label2.Text = "LV3-莲华";
                    }
                    else if (sdr == "初级用户")
                    {
                        label5.ForeColor = Color.SeaGreen;
                        label2.Text = "LV2-翡翠";
                    }
                    else
                    {
                        label5.ForeColor = Color.Black;
                        label2.Text = "LV1-曜石";
                    }
                }
                rdr.Close();
                try
                {
                    pictureBox3.Image = Image.FromFile(Application.StartupPath + sdr2);
                    pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox3.Visible = true;
                   
                }
                catch
                {
                    pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\" + "人1.jpg");
                    pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox3.Visible = false;
                }
                this.Visible = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            register rl = new register();
            rl.ShowDialog();
            if (flag == 1)
            {

                this.Visible = true;
            }
        }

        Class1 shopcarSrv= new Class1();

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            if (CIndex == 5)
            {
                int rowindex = e.RowIndex;
                Commodityid = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
                Tcommodity tl = new Tcommodity();
                tl.ShowDialog();
            }
            if (CIndex == 6)
            {
                if (Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    login ll = new login();
                    ll.ShowDialog();
                }
                else
                {
                    int rowindex = e.RowIndex;
                    Commodityid = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dataGridView1.Rows[rowindex].Cells[3].Value) < 1)
                    {
                        MessageBox.Show("该商品暂时没有库存");
                    }
                    else
                    {
                        dataGridView1.Rows[rowindex].Cells[3].Value = Convert.ToInt32(dataGridView1.Rows[rowindex].Cells[3].Value) - 1;
                        shopcarSrv.Add(login.username, Convert.ToInt32(newwin.Commodityid), 1);
                        Tshopcar sl = new Tshopcar();
                        sl.ShowDialog();

                    }

                }

            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {
            int CIndex = e.ColumnIndex;
            if (CIndex == 5)
            {
                int rowindex = e.RowIndex;
                Commodityid = dataGridView2.Rows[rowindex].Cells[0].Value.ToString();
                Tcommodity tl = new Tcommodity();
                tl.ShowDialog();
            }
            if (CIndex ==6)
            {
                if (Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    login ll = new login();
                    ll.ShowDialog();
                }
                else
                {
                    int rowindex = e.RowIndex;
                    Commodityid = dataGridView2.Rows[rowindex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dataGridView2.Rows[rowindex].Cells[3].Value) < 1)
                    {
                        MessageBox.Show("该商品暂时没有库存");
                    }
                    else
                    {
                        dataGridView2.Rows[rowindex].Cells[3].Value = Convert.ToInt32(dataGridView2.Rows[rowindex].Cells[3].Value) - 1;
                        shopcarSrv.Add(login.username, Convert.ToInt32(newwin.Commodityid), 1);
                        Tshopcar sl = new Tshopcar();
                        sl.ShowDialog();

                    }

                }

            }
        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)


        {
            int CIndex = e.ColumnIndex;
            if (CIndex ==5)
            {
                int rowindex = e.RowIndex;
                Commodityid = dataGridView3.Rows[rowindex].Cells[0].Value.ToString();
                Tcommodity tl = new Tcommodity();
                tl.ShowDialog();
            }
            if (CIndex == 6)
            {
                if (Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    login ll = new login();
                    ll.ShowDialog();
                }
                else
                {
                    int rowindex = e.RowIndex;
                    Commodityid = dataGridView3.Rows[rowindex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dataGridView3.Rows[rowindex].Cells[3].Value) < 1)
                    {
                        MessageBox.Show("该商品暂时没有库存");
                    }
                    else
                    {
                        dataGridView3.Rows[rowindex].Cells[3].Value = Convert.ToInt32(dataGridView3.Rows[rowindex].Cells[3].Value) - 1;
                        shopcarSrv.Add(login.username, Convert.ToInt32(newwin.Commodityid), 1);
                        Tshopcar sl = new Tshopcar();
                        sl.ShowDialog();

                    }

                }

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (login.username == "")
            {
                MessageBox.Show("尊贵的顾客请您先登录");
            }
            else
            {
                znew.Ncommodity ncommodity = new znew.Ncommodity();
                ncommodity.ShowDialog();
            }
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if(Aflag == false)
            {
                MessageBox.Show("请先登录");
            }
            else
            {
                people pe = new people();
                pe.ShowDialog();
            }
               
        }

        private void button6_Click(object sender, EventArgs e)
        {
            znew.订购 tr =new znew.订购();
            tr.ShowDialog();    
        }

        private void button7_Click(object sender, EventArgs e)
        {
            znew.Nwuliu liu = new znew.Nwuliu();
            liu.ShowDialog();
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
           
                znew.订购 nn = new znew.订购();
                nn.ShowDialog();
            
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            znew.订购 mm = new znew.订购();
            mm.ShowDialog();    
        }
    }
}
